here is the first draft of the source code and a compiled version in the attachment for testing purposes. I have for now implemented the ini file option.
BTW: I must admit, that in opposit to this my first idea was much more complicated. I thougth about an approach, where custom Dlls could be loaded and unloaded at runtime.
The source of the current solution is quite small and simple. There is only one unpleasant issue still there, I have also seen this in the source of Drift Gauge and in the tutorial sources from mikoweb. So I have to assume more dlls derived from this will have this issue too. Me must not call FreeLibrary inside the DllMain function (the MSDN says so). But for now it works. Nevertheless I would like to know where would be the appropriate location to call FreeLibrary, maybe in the Release function of the D3D8 interface?
@DragonCommando: Turbo Pascal, jeah, good language with a rigorous syntax check, very good to avoid a lot of unnecessary bugs. I loved this language, but it is a long time ago, that I have used it.
Oh, and believe: the attached dll will work with every addon D3D8.dll, they do not need to be recompiled. I have tested this with one of my own modded D3D8 wrapper Dlls.
regards
Sören
#include <windows.h>
// we do not take care of the type of the pointer
#define IDirect3D8 void
HINSTANCE g_hNextDll;
void LoadNextDll(void);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hNextDll = NULL;
break;
case DLL_PROCESS_DETACH:
if (g_hNextDll)
{
FreeLibrary(g_hNextDll);
g_hNextDll = NULL;
}
break;
}
return TRUE;
}
// Exported function (export declared in the .def file)
IDirect3D8* WINAPI Direct3DCreate8(UINT SDKVersion)
{
if (!g_hNextDll) LoadNextDll();
// Hooking IDirect3D Object from Original Library
typedef IDirect3D8 *(WINAPI* D3D8_Type)(UINT SDKVersion);
D3D8_Type D3DCreate8_fn = (D3D8_Type)GetProcAddress(g_hNextDll, "Direct3DCreate8");
if (!D3DCreate8_fn)
{
ExitProcess(0); // exit the hard way
}
return D3DCreate8_fn(SDKVersion);
}
void LoadNextDll(void)
{
char path2dll[MAX_PATH];
char path2ini[MAX_PATH];
if (GetModuleFileName(NULL, path2ini, sizeof(path2ini)))
{
// get the path of the LFS folder
while ((path2ini[strlen(path2ini) - 1] != '\\') && (strlen(path2ini) > 0))
path2ini[strlen(path2ini) - 1] = 0;
strcat(path2ini, "D3D8.ini");
GetPrivateProfileString("D3D8Wrapper", "ProxyDll", "",
path2dll, sizeof(path2dll), path2ini);
if (strlen(path2dll) > 0)
{
g_hNextDll = LoadLibrary(path2dll);
if (g_hNextDll)
return;
}
}
// no ini file there or loading the custom dll failed, so let's load the systems d3d8.dll
GetSystemDirectory(path2dll, MAX_PATH);
strcat(path2dll, "\\d3d8.dll");
g_hNextDll = LoadLibrary(path2dll);
if (!g_hNextDll)
{
ExitProcess(0); // exit the hard way
}
}